package com.woniuxy.charin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.charin.model.entity.Customers;
import com.woniuxy.charin.service.CustomersService;
import com.woniuxy.charin.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;


@RestController
@RequestMapping("/customers")
@Api(tags = "客户管理")
@Slf4j
public class CustomerController {

    @Autowired
    private JwtUtil jwtUtil;  //要实现会话跟踪，从Token中获取用户id
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CustomersService customerService;

    @Autowired
    private UploadController uploadController;

    // ... existing code ...

    @PostMapping("/uploadAvatar")
    @ApiOperation("上传用户头像")
    public ResponseEntity<Boolean> uploadAvatar(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id) {
        try {
            // 调用已有的文件上传接口
            ResponseEntity<String> uploadResult = uploadController.upload(file);

            if (uploadResult.getStatusCode().is2xxSuccessful()) {
                // 更新用户头像URL
                Customers customer = new Customers();
                customer.setId(id.intValue());
                customer.setAvatar(uploadResult.getBody());
                boolean result = customerService.updateById(customer);
                return ResponseEntity.ok(result);
            }
            return ResponseEntity.badRequest().build();
        } catch (Exception e) {
            log.error("上传头像失败", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @PutMapping("/updateAvatar")
    @ApiOperation("更新用户头像")
    public ResponseEntity<Boolean> updateAvatar(@RequestParam("file") MultipartFile file,
                                                @RequestParam("id") Long id,
                                                @RequestParam("oldAvatar") String oldAvatar) {
        try {
            // 从旧头像URL中提取文件名
            String oldFileName = oldAvatar.substring(oldAvatar.lastIndexOf("/") + 1);

            // 调用已有的更新文件接口
            ResponseEntity<String> updateResult = uploadController.update(file, oldFileName);

            if (updateResult.getStatusCode().is2xxSuccessful()) {
                // 更新用户头像URL
                Customers customer = new Customers();
                customer.setId(id.intValue());
                customer.setAvatar(updateResult.getBody());
                boolean result = customerService.updateById(customer);
                return ResponseEntity.ok(result);
            }
            return ResponseEntity.badRequest().build();
        } catch (Exception e) {
            log.error("更新头像失败", e);
            return ResponseEntity.badRequest().build();
        }
    }
    /**
     * 查询所有客户
     */
    @PostMapping("/findAll")
    @ApiOperation("查询所有客户,按积分排序")
    public ResponseEntity<Page<Customers>> findTopSales(@RequestBody Map<String, Object> params) {
        int pageNum = (int) params.get("pageNum");
        int pageSize = (int) params.get("pageSize");
        String customerName = (String) params.get("customerName");
        String phoneNumber = (String) params.get("phoneNumber");
        String accountLevel = (String) params.get("accountLevel");

        Page<Customers> page = customerService.findTopSales(pageNum, pageSize, customerName, phoneNumber, accountLevel);
        return ResponseEntity.ok(page);
    }

    /**
     * 添加客户
     */
    @PostMapping("/add")
    @ApiOperation("添加客户")
    public ResponseEntity<Boolean> add(@RequestBody Customers customers) {
        boolean result = customerService.save(customers);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据id删除客户
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据id删除客户")
    public ResponseEntity<Boolean> delete(@PathVariable Long id) {
        boolean result = customerService.removeById(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 修改客户信息
     */
    @PutMapping("/update")
    @ApiOperation("修改客户信息")
    public ResponseEntity<Boolean> update(@RequestBody Customers customers) {
        boolean result = customerService.updateById(customers);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据id查询客户
     */
    @GetMapping("/find/{id}")
    @ApiOperation("根据id查询客户")
    public ResponseEntity<Customers> findById(@PathVariable Long id) {
        Customers customers = customerService.getById(id);
        return ResponseEntity.ok(customers);
    }

}